package com.example.product_service.controller;

import com.example.product_service.domain.Product;
import com.example.product_service.service.ProductService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.concurrent.TimeUnit;

@RestController
@RequestMapping("/api/v1/product")
public class ProductController {

    @Value("${server.port}")
    private String port;

    private ProductService productService;

    @Autowired
    public ProductController(ProductService productService) {
        this.productService = productService;
    }

    /**
     * 获取所有商品列表
     *
     * @return
     */
    @GetMapping("/list")
    public Object list() {
        return productService.listProduct();
    }

    /**
     * 根据id查找商品详情
     *
     * @param id 商品id
     * @return
     */
    @GetMapping("/find")
    public Object findById(@RequestParam("id") int id) {
//        try {
//            TimeUnit.SECONDS.sleep(2);
//        } catch (InterruptedException e) {
//            e.printStackTrace();
//        }

        Product product = productService.findById(id);
        Product result = new Product();
        // 复制对象属性的工具类
        BeanUtils.copyProperties(product, result);
        result.setName(result.getName() + " data from port=" + port);
        return result;
    }
}
